package asf.sections
{
	import asf.core.Application;
	import asf.core.ApplicationParameters;
	import asf.core.ApplicationParser;
	import asf.core.BaseApplication;
	import asf.log.LogLevel;
	import asf.utils.Align;
	import asf.views.View;
	
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.adobe.utils.StringUtil;
	
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;

	public class Section extends BaseApplication
	{
		private var node:XML;
		private var app:Application;
		
		private var _view:View;
		private var _loadAtStart:Boolean = false;
		private var _isLoadingInBackground:Boolean = false;
		
		private var _params:ApplicationParameters;
		
		public function Section( baseNode:XML, p_app:Application )
		{
			super( p_app );
			
			node = baseNode;
			app = p_app;
			
			_params = new ApplicationParameters( baseNode, app.loaderInfo, this );
			
			this.loadAtStart = String( node.@loadAtStart ) == "true" || String( node.loadAtStart ) == "true";
			this.rawXML = baseNode;
		}
		
		public function get parameters( ):ApplicationParameters
		{
			return _params;
		}
		
		public function getParsedValueOf( raw:String ):String
		{
			return ApplicationParser.parse( raw, node, this );
		}
		
		public function startBackgroundLoading( ):void
		{
			if( loadInBackground )
			{
				app.log( id, LogLevel.INFO_3 );
				
				load( );
				this._isLoadingInBackground = true;
			}
		}
		
		public function get isLoadingInBackground( ):Boolean
		{
			return this._isLoadingInBackground;
		}
		
		public function get loadAtStart( ):Boolean
		{
			return _loadAtStart;
		}
		
		public function get loadInBackground( ):Boolean
		{
			return String( node.@loadInBackground ) == "true" || String( node.loadInBackground ) == "true";
		}
		
		public function get keepDependencies( ):Boolean
		{
			return String( node.@keepDependencies ) == "true" || String( node.keepDependencies ) == "true";
		}
		
		public function set loadAtStart( value:Boolean ):void
		{
			_loadAtStart = value;
		}
		
		public function load( ):void
		{
			if( this.isLoading ) return;
			if( this.isLoaded )
			{
				app.log( "already loaded: " + id, LogLevel.INFO_3 );
				
				this.dependenciesOnComplete( null );
				return; 
			}
			
			app.log( id, LogLevel.INFO_3 );
			
			var dFiles:Array = getDictionaryFiles( node );
			var dFile:String;
			var depend:XMLList = node.copy( ).dependencies;
			var n:uint = 0;
			
			if( dFiles.length > 0 )
			{
				for each( dFile in dFiles )
				{
					depend.file += new XML( this.formatDependencyNode( "$_dict_" + n, dFile, "xml" ) );
					
					n++;
				}
			}
			
			this.initDependencies( depend, node.@useOwnContext == "true" );
		}
		
		protected override function dependenciesOnComplete(evt:BulkProgressEvent):void
		{
			this._isLoadingInBackground = false;
			super.dependenciesOnComplete( evt );
		}
		
		public function set view( value:View ):void
		{
			_view = value;
			
			Align.remove( _view );
			applyAlign( );
		}
		
		private function applyAlign( ):void
		{
			var anchors:uint = 0;
			var align:String = align.toLowerCase( );
			var props:Object = new Object( );
			
			if( StringUtil.trim( align ) != "" )
			{
				if( align.indexOf( "top" ) != -1 ) anchors += Align.TOP;
				if( align.indexOf( "bottom" ) != -1 ) anchors += Align.BOTTOM;
				if( align.indexOf( "middle" ) != -1 ) anchors += Align.MIDDLE;
				if( align.indexOf( "left" ) != -1 ) anchors += Align.LEFT;
				if( align.indexOf( "center" ) != -1 ) anchors += Align.CENTER;
				if( align.indexOf( "right" ) != -1 ) anchors += Align.RIGHT;
				if( align.indexOf( "none" ) != -1 ) anchors += Align.NONE;
				
				Align.add( _view, anchors, positioning );
			}
		}
		
		public function get view( ):View
		{
			return _view;
		}
		
		public function get application( ):Application
		{
			return this.app;
		}
		
		public function get id( ):String
		{
			return app.getParsedValueOf( node.@id || node.id );
		}
		
		public function get menuLabel( ):String
		{
			return app.getParsedValueOf( node.@menuLabel || node.menuLabel || node.@id || node.id );
		}
		
		public function get align( ):String
		{
			return node.@align || node.align;
		}
		
		public function get layer( ):Sprite
		{
			var layerName:String = app.getParsedValueOf( node.@layer || node.layer );
			
			if( layerName != "" )
			{
				return app.layers[ layerName ];
			}
			else
			{
				return app.navigation.baseLayer;
			}
		}
		
		public function get useOwnContext( ):Boolean
		{
			return node.@useOwnContext == "true" || node.useOwnContext == "true";
		}
		
		public function get viewClassName( ):String
		{
			return node.@viewClass || node.viewClass;
		}
		
		public function get viewClass( ):Class
		{
			return getDefinitionByName( viewClassName ) as Class;
		}
		
		public function get positioning( ):SectionPositioning
		{
			return new SectionPositioning( node.positioning, app );
		}
		
		public function get extras( ):XMLList
		{
			return node.extras;
		}
		
		public override function dispose( unloadDependencies:Boolean = true ):void
		{
			app.log( id, LogLevel.INFO_3 );
			super.dispose( ! this.keepDependencies ); 
		}
	}
}